package cn.broadsense.biz.service.impl;

import cn.broadsense.biz.bo.LayerAreaBo;
import cn.broadsense.biz.bo.LayerBo;
import cn.broadsense.biz.convert.LayerConvert;
import cn.broadsense.biz.domain.Channel;
import cn.broadsense.biz.domain.LayerArea;
import cn.broadsense.biz.domain.LayerAreaChannel;
import cn.broadsense.biz.dto.LayerAreaDto;
import cn.broadsense.biz.service.ChannelService;
import cn.broadsense.biz.service.LayerAreaChannelService;
import cn.broadsense.biz.service.LayerAreaService;
import cn.broadsense.common.exception.ServiceException;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.broadsense.biz.domain.Layer;
import cn.broadsense.biz.service.LayerService;
import cn.broadsense.biz.mapper.LayerMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author bs
 * @description 针对表【t_layer(图层表)】的数据库操作Service实现
 * @createDate 2024-09-10 11:18:08
 */
@RequiredArgsConstructor
@Service
public class LayerServiceImpl extends ServiceImpl<LayerMapper, Layer>
        implements LayerService {

    private final LayerAreaService layerAreaService;

    private final LayerAreaChannelService areaChannelService;

    private final LayerConvert layerConvert;

    private final ChannelService channelService;

    @Override
    public void saveLayer(LayerBo layerBo) {
        Layer layer = new Layer();
        layer.setSort(layerBo.getSort());
        layer.setName(layerBo.getName());
        super.save(layer);
    }

    @Override
    public void updateLayer(LayerBo layerBo) {
        Layer layer = super.getById(layerBo.getId());
        if (layer == null) {
            throw new ServiceException("图层不存在");
        }
        layer.setSort(layerBo.getSort());
        layer.setName(layerBo.getName());
        super.updateById(layer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveLayerArea(LayerAreaBo layerAreaBo) {
        LayerArea layerArea = layerConvert.areaBoToLayerArea(layerAreaBo);
        List<LayerAreaChannel> areaChannelList = getChannelList(layerAreaBo, layerArea);
        layerAreaService.save(layerArea);
        if (CollUtil.isNotEmpty(areaChannelList)) {
            areaChannelList.forEach(areaChannel -> areaChannel.setLayerAreaId(layerArea.getId()));
            areaChannelService.saveBatch(areaChannelList);
        }
        //新增图层数量
        increaseLayerAreaQty(layerArea.getLayerId(), 1);
    }


    @Override
    public void updateLayerArea(LayerAreaBo layerAreaBo) {
        LayerArea layerArea = layerConvert.areaBoToLayerArea(layerAreaBo);
        areaChannelService.remove(Wrappers.<LayerAreaChannel>lambdaQuery()
                .eq(LayerAreaChannel::getLayerAreaId, layerArea.getId())
        );
        List<LayerAreaChannel> areaChannelList = getChannelList(layerAreaBo, layerArea);
        if (CollUtil.isNotEmpty(areaChannelList)) {
            areaChannelList.forEach(areaChannel -> areaChannel.setLayerAreaId(layerArea.getId()));
            areaChannelService.saveBatch(areaChannelList);
        }
        layerAreaService.updateById(layerArea);
    }

    @Override
    public List<LayerAreaDto> getLayerAreas(Long layerId, String areaName, Date startTime, Date endTime) {
        return baseMapper.listChannel(layerId, areaName, startTime, endTime);
    }

    @Override
    public LayerAreaDto getLayerArea(Long areaId) {
        LayerArea layerArea = layerAreaService.getById(areaId);
        if (layerArea != null) {
            LayerAreaDto layerAreaDto = layerConvert.toLayerAreaDto(layerArea);
            List<LayerAreaChannel> areaChannelList = areaChannelService.list(Wrappers.<LayerAreaChannel>lambdaQuery()
                    .eq(LayerAreaChannel::getLayerAreaId, areaId)
            );
            layerAreaDto.setChannelList(areaChannelList);
            return layerAreaDto;
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeArea(Long layerAreaId) {
        LayerArea layerArea = layerAreaService.getById(layerAreaId);
        if (layerArea == null) {
            throw new ServiceException("图层区域不存在");
        }
        areaChannelService.remove(Wrappers.<LayerAreaChannel>lambdaQuery()
                .eq(LayerAreaChannel::getLayerAreaId, layerAreaId)
        );
        layerAreaService.removeById(layerAreaId);
        //减少图层数量
        increaseLayerAreaQty(layerArea.getLayerId(), -1);
    }

    @Override
    public List<Layer> listAll(String name) {
        return baseMapper.listAll(name);
    }

    @Override
    public void increaseLayerAreaQty(Long layerId, Integer qty) {
        Layer layer = super.getById(layerId);
        if (layer == null) {
            throw new ServiceException("图层不存在");
        }
        this.baseMapper.increaseLayerAreaQty(layerId, qty);
    }

    private List<LayerAreaChannel> getChannelList(LayerAreaBo layerAreaBo, LayerArea layerArea) {
        List<LayerAreaChannel> areaChannelList = new ArrayList<>();
        if (CollUtil.isNotEmpty(layerAreaBo.getChannelIds())) {
            List<Channel> channelList = channelService.listByIds(layerAreaBo.getChannelIds());
            if (CollUtil.isNotEmpty(channelList)) {
                for (Channel channel : channelList) {
                    LayerAreaChannel areaChannel = new LayerAreaChannel();
                    areaChannel.setChannelId(channel.getId());
                    areaChannel.setLayerAreaId(layerArea.getId());
                    areaChannel.setChannelCode(channel.getChannelCode());
                    areaChannel.setChannelName(channel.getChannelName());
                    areaChannelList.add(areaChannel);
                }
            }
        }
        return areaChannelList;
    }
}




